package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnCode;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.Button;
import com.antdesignbackend.backend.entity.entityVo.DisinfectVo;
import com.antdesignbackend.backend.entity.entityVo.MonthReportRecordVo;
import com.antdesignbackend.backend.service.DisinfectService;
import com.antdesignbackend.backend.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/disinfect")
public class DisinfectController extends BaseController {
    @Autowired
    DisinfectService disinfectService;

    @Autowired
    PermissionService permissionService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Integer menuId;

    @GetMapping("/index")
    @AuthToken
    public String ProductionIndex(String menuId) {
        this.menuId = Integer.parseInt(menuId);
        return "records/disinfect/index";
    }
    /**
     * 分页查询
     * @param
     * @return
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody DisinfectVo disinfectVo, HttpServletRequest request) {
        ReturnVO result = new ReturnVO();
        List<DisinfectVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<DisinfectVo> queryWrapper = new QueryWrapper<>();
            if (disinfectVo.getDisinfectName() != null && !disinfectVo.getDisinfectName().equals("")){
                queryWrapper.like("disinfectName",disinfectVo.getDisinfectName());
            }
            if (disinfectVo.getTimeInterval() != null && !disinfectVo.getTimeInterval().equals("")){
                String[] str=disinfectVo.getTimeInterval().split(" - ");
                String star=str[0];
                String end=str[1];
                queryWrapper.between("disinfectTime",star,end);
            }
            queryWrapper.orderByDesc("disinfectTime").orderByDesc("createTime");
            queryWrapper.eq("eid",getLoginEnterpriseId());
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=disinfectService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= disinfectService.search(queryWrapper,new Page<>(disinfectVo.getPageIndex(),disinfectVo.getPageSize()));
                //得到当前菜单具备的功能权限
                //获得当前角色
//                Object roleID = request.getSession().getAttribute("RoleID");
//
//                datalist.forEach(model -> model.setButtonList(list));
            }
            result.setData(datalist);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return result;
    }
    @PostMapping("/tableSearchMe/{eid}")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearchMe(@PathVariable("eid") int eid) {
        ReturnVO result = new ReturnVO();
        List<DisinfectVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<DisinfectVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("disinfectTime");
            queryWrapper.eq("eid",eid);
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=disinfectService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= disinfectService.search(queryWrapper,new Page<>(1,10));

            }
            result.setData(datalist);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return result;
    }
    /**
     * 添加
     * @param disinfectVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @AuthToken
    public ReturnVO add(@RequestBody DisinfectVo disinfectVo) {
        try {
            disinfectVo.setUid(getLoginUserId());
            disinfectVo.setCreateTime(new Date());
            disinfectVo.setEid(getLoginEnterpriseId());
            disinfectService.save(disinfectVo);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }
    /**
     * 修改
     * @param disinfectVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    @AuthToken
    public ReturnVO edit(@RequestBody DisinfectVo disinfectVo) {
        try {
            disinfectService.updateById(disinfectVo);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    /**
     * 删除
     * @param disinfectVo
     * @return
     */
    @PostMapping("/del")
    @ResponseBody
    @AuthToken
    public ReturnVO del(@RequestBody DisinfectVo disinfectVo) {
        try {
            disinfectService.removeById(disinfectVo.getDisinfectId());
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }
}
